﻿using UnityEngine;
using UnityEngine.UI;
using GameFramework;

namespace MYSTARFORCE
{
    public class AboutForm : UGuiForm
    {
        /// <summary>
        /// 容器节点的rect transform组件
        /// 我们会将整个节点从下往上移动，因此选择这个组件就可以了
        /// </summary>
        [SerializeField]
        private RectTransform m_RectTransform = null;

        /// <summary>
        /// 滚动速度
        /// </summary>
        [SerializeField]
        private float m_ScrollSpeed = 1f;

        /// <summary>
        /// 初始坐标为0
        /// </summary>
        private float m_InitPosition = 0f;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            // 查找父节点UI Form Instances的CanvasScaler组件
            CanvasScaler canvasScaler = GetComponentInParent<CanvasScaler>();
            if (canvasScaler == null)
            {
                Log.Warning("Can not find CanvasScaler component");
                return;
            }

            // 设置初始位置
            // 初始位置等于设计分辨率的X数值 * 屏幕高度和宽度之比 * -0.5
            m_InitPosition = -0.5f * canvasScaler.referenceResolution.x * Screen.height / Screen.width;
        }
        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);

            // 打开界面的时候，把content放到最下方
            m_RectTransform.SetLocalPositionY(m_InitPosition);
        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);

            // 向上移动
            m_RectTransform.AddLocalPositionY(m_ScrollSpeed * elapseSeconds);

            if(m_RectTransform.localPosition.y > m_RectTransform.sizeDelta.y - m_InitPosition)
            {
                m_RectTransform.SetLocalPositionY(m_InitPosition);
            }
        }
        protected override void OnClose(object userData)
        {
            base.OnClose(userData);
        }

    }
}